// pages/mine/mine.js
import request from '../../utils/network.js'
Page({

  data: {
    spinShow: true,
    index: 0,
    activeName: '0',
    allOrder: [], //全部
    toDeliver: [], //待发货
    onFinish: [], //已完成
    goodsMap: {},
    list: [],
    goods: []

  },
  onLoad: function(options) {
    var token = wx.getStorageSync('token')
    this.getAllOrder(token)
  },
  //获取所有订单
  getAllOrder(token) {
    request({
      url: 'https://api.it120.cc/kehuafu/order/list',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 请求头
      },
      data: {
        token: token
      }
    }).then(res => {
      console.log(res)
      if (res.data.msg == '暂无数据') {
        this.setData({
          spinShow: false
        })
        wx.stopPullDownRefresh() //停止下拉刷新
        return
      }
      var allOrder = []
      var toDeliver = []
      var onFinish = []
      var goods = []
      allOrder.push(...res.data.data.orderList)
      console.log(allOrder)
      this.setData({
        goodsMap: res.data.data.goodsMap
      })
      var goodsMap = this.data.goodsMap
      for (var i = 0; i < allOrder.length; i++) {
        if (allOrder[i].statusStr == '待发货') {
          toDeliver.push(allOrder[i])
        } else if (allOrder[i].statusStr == '待评价') {
          onFinish.push(allOrder[i])
        }
        if (goodsMap[allOrder[i].id] != null) {
          goods.push(goodsMap[allOrder[i].id])
        }
      }
      console.log(goods)
      this.setData({
        [`allOrder`]: allOrder,
        [`toDeliver`]: toDeliver,
        [`onFinish`]: onFinish,
        [`list`]: allOrder,
        [`goods`]: goods,
        spinShow: false,
        index: 0
      })
      wx.stopPullDownRefresh() //停止下拉刷新
    }).catch(err => {
      console.log(err)
    })
  },
  //切换tag
  change(event) {
    var index = event.detail.index
    var goods = []
    switch (index) {
      case 0:
        var allOrder = this.data.allOrder
        for (var i = 0; i < allOrder.length; i++) {
          if (this.data.goodsMap[allOrder[i].id] != null) {
            goods.push(this.data.goodsMap[allOrder[i].id])
          }
        }
        this.setData({
          [`list`]: allOrder,
          [`goods`]: goods
        })
        break;
      case 1:
        var toDeliver = this.data.toDeliver
        for (var i = 0; i < toDeliver.length; i++) {
          if (this.data.goodsMap[toDeliver[i].id] != null) {
            goods.push(this.data.goodsMap[toDeliver[i].id])
          }
        }
        this.setData({
          [`list`]: toDeliver,
          [`goods`]: goods
        })
        break;
      case 2:
        var onFinish = this.data.onFinish
        for (var i = 0; i < onFinish.length; i++) {
          if (this.data.goodsMap[onFinish[i].id] != null) {
            goods.push(this.data.goodsMap[onFinish[i].id])
          }
        }
        this.setData({
          [`list`]: onFinish,
          [`goods`]: goods
        })
        break;
    }
  },
  //折叠变化
  onChange(event) {
    this.setData({
      activeName: event.detail
    });
  },
  //返回首页
  backHome() {
    console.log("返回首页")
    wx.navigateBack()
  },
  //下拉刷新
  onPullDownRefresh() {
    console.log("下拉获取最新订单")
    var token = wx.getStorageSync('token')
    this.getAllOrder(token)
  }
})